﻿using System.Configuration;
using System.Text.RegularExpressions;
using System.Web;

namespace Web
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler
    {
        public static string RemoveHtmlTags(string inputStr)
        {
            if (string.IsNullOrEmpty(inputStr)) return null;
            Regex regex = new Regex(@"<[^>]+>|</[^>]+>");
            return regex.Replace(inputStr, "");
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string res = "ok";
            var name = RemoveHtmlTags(context.Request.Form["name"]);
            var phone = RemoveHtmlTags(context.Request.Form["tel"]);
            var yinglou = RemoveHtmlTags(context.Request.Form["yinglou"]);
            var content = RemoveHtmlTags(context.Request.Form["content"]);
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(yinglou) ||
                string.IsNullOrEmpty(content))
            {
                res = "请输入完整信息！";
            }
            else
            {
                var html = "<table width='800' cellspace='0' cellpadding='0' align='center' border='1' bordercolor='black'>";
                html += "<tr><td>姓名</td><td>" + name + "</td></tr>";
                html += "<tr><td>电话</td><td>" + phone + "</td></tr>";
                html += "<tr><td>影楼</td><td>" + yinglou + "</td></tr>";
                html += "<tr><td>内容</td><td>" + content + "</td></tr>";
                html += "</table>";

                var recieve = ConfigurationManager.AppSettings["RecieveEmailAccount"];
                new EmailService().Send(recieve, "有一封来自熊宝贝官网的邮件", html);
            }

            context.Response.Write(res);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}